package com.sinosoft.web.common.tag;

import cn.com.sinux.spring.commons.SpringUtils;
import com.sinosoft.hydra.um.constant.BaseConstant;
import com.sinosoft.hydra.um.domain.permission.SysResource;
import com.sinosoft.hydra.um.services.SysResourceService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/20.
 */
public class ButtonTag extends javax.servlet.jsp.tagext.TagSupport {

    private String permission;
    private String dataKey;
    private String dataIcon;
    private String dataId;
    private String dataParam;


    public int doStartTag() throws JspException {
        try {
            JspWriter out = this.pageContext.getOut();
            Subject subject = SecurityUtils.getSubject();
            if (!subject.isPermitted(permission)) {
                return SKIP_BODY;
            }
            SysResourceService sysResourceService = SpringUtils.getBean(SysResourceService.class);
            SysResource resource = sysResourceService.findByPermission(permission);
            if (resource == null) {
                return SKIP_BODY;
            }
            StringBuilder stringBuilder = new StringBuilder("<a ");
            List<String> attrs = new LinkedList<String>();
            attrs.add("plain=\"true\"");
            if (BaseConstant.ResourceType.MENU.getValue().equals(resource.getResourceType())) {
                attrs.add("data-target=\"link\"");
            }
            attrs.add("data-href=\"" + pageContext.getServletContext().getContextPath() + resource.getResourceUrl() + "\"");
            attrs.add("data-key=\"" + dataKey + "\"");
            attrs.add("iconCls=\"" + dataIcon + "\"");
            if (StringUtils.isNotBlank(dataId)) {
                attrs.add("data-id=\"" + dataId + "\"");
            }
            if (StringUtils.isNotBlank(dataParam)) {
                attrs.add("data-param=\"" + dataParam + "\"");
            }
            stringBuilder.append(StringUtils.join(attrs, " "));
            stringBuilder.append(">").append(resource.getResourceName()).append("</a>");
            out.println(stringBuilder.toString());
        } catch (Exception e) {
            throw new JspException(e.getMessage());
        }
        return SKIP_BODY;
    }


    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }


    public void release() {
        super.release();
    }

    public void setDataKey(String dataKey) {
        this.dataKey = dataKey;
    }

    public void setDataIcon(String dataIcon) {
        this.dataIcon = dataIcon;
    }

    public void setDataId(String dataId) {
        this.dataId = dataId;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    public void setDataParam(String dataParam) {
        this.dataParam = dataParam;
    }
}
